package com.ibix.chj.guazi;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import com.ibix.chj.model.CarBrand;
import com.ibix.chj.model.CarListByCharacter;
import com.ibix.chj.model.CarSeries;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

public class GuaziPageProcessor implements PageProcessor{
	
	// ����һ��ץȡ��վ��������ã��������롢ץȡ��������Դ�����
    private Site site = Site.me().setRetryTimes(3).setSleepTime(5000);
    
	private ConcurrentHashMap<String, CarListByCharacter> map = new ConcurrentHashMap<String, CarListByCharacter>();

	public void process(Page page) {
		if (page.getUrl().toString().equals("https://www.guazi.com/bj/"))
		{
			ParseDataTo(page);
		} else {
			
		}
		
	}

	private void ParseDataTo(Page page) {
		Selectable leftData = page.getHtml().xpath("//table[@class='tbl']").$("tbody").$("tr");
		ParseDataToMap(leftData, map, page);
		
		Selectable rightData = page.getHtml().xpath("//table[@class='tbr']").$("tbody").$("tr");
		ParseDataToMap(rightData, map, page);
	}

	private void ParseDataToMap(Selectable leftData, ConcurrentHashMap<String, CarListByCharacter> map, Page page) {
		List<Selectable> leftList = leftData.nodes();
		for (int i=0,count=leftList.size();i<count;i++)
		{
			Selectable node = leftList.get(i);
			String upperCharacter = node.xpath("//td[@class='zm']").regex(">[A-Z]<").replace(">", "").replace("<", "").toString();
			System.out.println("=================[" + upperCharacter + "]=================");
			
			CarListByCharacter chByCharacter = new CarListByCharacter();
			chByCharacter.setCharacter(upperCharacter);
			chByCharacter.setBrands(new ArrayList<CarBrand>());
			map.put(upperCharacter, chByCharacter);
			
			List<Selectable> aNodeList = node.$("td").nodes().get(1).$("a").nodes();
			
			
			for (int j=0,size=aNodeList.size();j<size;j++) {
				Selectable aNode = aNodeList.get(j);
				
				String name = aNode.regex(">.*<").replace(">", "").replace("<", "").toString().trim();
				String link = aNode.regex("https://www\\.guazi\\.com/bj/.*/\"").replace("\"",  "").toString().trim();
				
				if (name == null || name.equals("null"))
				{
					System.err.println("name is null");
				}
				
				if (link == null || link.equals("null"))
				{
					System.err.println("name is null");
				}
				
				CarBrand brand = new CarBrand();
				brand.setName(name);
				brand.setLink(link);
				brand.setSeries(new ArrayList<CarSeries>());
				
				System.out.println("name:" + name + ",link:" + link);
				
				page.addTargetRequest(link);
			}
			
			System.out.println("=====================================");
		}
	}

	public Site getSite() {
		return site;
	}
}
